﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BO;

namespace ProjectQuaDep
{
    public partial class DownloadDriver : System.Web.UI.Page
    {
        string LISTDOWSFORMAT = "<li><a href=\"{0}\" title=\"{2}\"><img src=\"{1}\" alt=\"{2}\" /></a><p><a {7} href=\"{5}\" {8} >{3}{6}</a></p><h4><a href=\"{0}\">{2}</a></h4><span>{4}</span></li>";
        string KeySearch = "";
        int pageSize = 10;
        int pageIndex = 1;
        int IMG_WIDTH = 100;
        int IMG_HEIGHT = 80;
        protected void Page_Load(object sender, EventArgs e)
        {
            pageIndex = lib.PageIndex;
            KeySearch = lib.KeySearch;
            DataTable dt = BO.Download.DownloadBO.GetListDownloadBO(KeySearch, pageSize.ToString(), pageIndex.ToString());
            if (dt.Rows.Count > 0)
            {
                ltTitleSearch.Text = KeySearch;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string newsID = dt.Rows[i][Const.tbProducts.id].ToString();
                    string title = Utils.EnCodeString(dt.Rows[i][Const.tbProducts.Title].ToString());
                    string href = dt.Rows[i][Const.tbProducts.Url].ToString();//Utils.BuildNewsDetail(newsID, dt.Rows[i][Const.tbProducts.Url].ToString());
                    string avatar = Utils.GetOneImagesByArr(dt.Rows[i][Const.tbProducts.Avatar].ToString());
                    avatar = Utils.CropImage(IMG_WIDTH, IMG_HEIGHT, avatar);
                    string linkDownload = dt.Rows[i][Const.tbDownloadProduct.LinkDownload].ToString();
                    string NameDownload = dt.Rows[i][Const.tbDownloadProduct.Name].ToString();
                    string iconType = "";
                    int typeProduct = int.Parse(dt.Rows[i][Const.tbDownloadProduct.TypeProduct].ToString());
                    string linkUrl = linkDownload;
                    string Onclick = "";
                    string target = "target=\"_blank\"";
                    if (typeProduct == 2)
                    {
                        iconType = "<img title=\"Catalog (data sheet ) excel\" style=\"width:auto;height:60px;float:right\" src=\"/Images/icon/icon-catalog.jpg\" />";
                    }
                    else if (typeProduct == 3)
                    {
                        iconType = "<img title=\"Hướng dẫn sử dụng\" style=\"width:auto;height:60px;float:right\" src=\"/Images/icon/icon-guide.jpg\" />";
                    }
                    else if (typeProduct == 4)
                    {
                        iconType = "<img title=\"Review sản phẩm\" style=\"width:auto;height:60px;float:right\" src=\"/Images/icon/review.jpg\" />";
                        linkUrl = "javascript:void(0)";
                        Onclick = "onclick=\"PopupWindowCenter('/ReviewVideo.aspx?url=" + linkDownload + "','',560,315)\"";
                        target = "";
                    }
                    else
                    {
                        iconType = "<img title=\"Phần mềm\" style=\"width:auto;height:60px;float:right\" src=\"/Images/icon/icon-download.jpg\" />";
                    }

                    string sTime = DateTime.Parse(dt.Rows[i][Const.tbProducts.DistributionDate].ToString()).ToString("dd/MM/yyyy");
                    ltListNews.Text += string.Format(LISTDOWSFORMAT, href, avatar, title, NameDownload, sTime, linkUrl, iconType, Onclick, target);

                }
                //set phan trang
                int totalRows = int.Parse(BO.Download.DownloadBO.CountGetListDownloadBO(KeySearch).Rows[0][Const.tbDownloadProduct.TongBanGhi].ToString());
                PaddingSearch1.PageSize = pageSize;
                int SumPage = totalRows % pageSize == 0 ? totalRows / pageSize : (totalRows / pageSize) + 1;
                PaddingSearch1.SumPage = SumPage;
                //if (PaddingSearch1.SumPage <= 1)
                //{
                //    PaddingSearch1.Visible = false;
                //}
                this.PaddingSearch1.LinkSite = "thu-vien-download.htm?PageType=5&KeySearch=" + KeySearch + "&PageIndex";
                this.PaddingSearch1.Padding(pageIndex);


                PaddingSearch1.Padding(pageIndex);
            }
            else
            { ltTitleSearch.Text = KeySearch + " không có kết quả nào"; }
            //set meta
            string Trang = "";
            if (pageIndex > 1)
            {
                Trang = " | Trang " + pageIndex;
            }
            Page.Title = "Kho download của hồng thủy" + Trang;
            string hDesc = "Kho download của hồng thủy" + Trang;
            string hKey = "Kho download của hồng thủy";
            BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Description.Id, BAL.Const.MetaAttributes.Description.Name, hDesc);
            BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Keywords.Id, BAL.Const.MetaAttributes.Keywords.Name, hKey);
            BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.NewKeywords.Id, BAL.Const.MetaAttributes.NewKeywords.Name, hKey);
        }
    }
}